package Test28;

import java.util.ArrayList;
import java.util.Scanner;

public class Manager {
    Scanner INPUT = new Scanner(System.in);
    ArrayList<Student> stu= new ArrayList<>();

    public void show() {
        add();
        while (true) {
            System.out.println("1.添加学生信息");
            System.out.println("2.显示学生信息");
            System.out.println("3.修改学生成绩");
            System.out.println("4.删除学生信息");
            System.out.println("5.按照学生年龄进行排序");
            System.out.println("6.按照学生成绩进行排序");
            System.out.println("0.退出");
            System.out.println("请输入您的选择:");
            int choice = INPUT.nextInt();
            switch (choice) {
                case 1:
                    add();
                    break;
                case 2:
                    Select();
                    break;
                case 3:
                    updateScore();
                    break;
                case 4:
                    delete();
                    break;
                case 5:
                    sortAge();
                    break;
                case 6:
                    sort();
                    break;
                case 0:
                    System.exit(0);
                    break;
                default:
                    System.out.println("输入错误，请重新输入");
            }
        }
    }
    public void IfFlag() {


    }
    public void add() {
        // 添加学生信息
        Student stu1 = new Student(1, "张三", 18, 100);
        Student stu2 = new Student(2, "李四", 19, 90);
        Student stu3 = new Student(3, "王五", 20, 80);
        Student stu4 = new Student(4, "赵六", 21, 67);
        Student stu5 = new Student(5, "张三一", 20, 88);
        // 添加到集合中
        stu.add(stu1);
        stu.add(stu2);
        stu.add(stu3);
        stu.add(stu4);
        stu.add(stu5);
        Select();
    }

    public void Select() {
        for (int i = 0; i < stu.size(); i++) {
            System.out.println(stu.get(i));
        }
    }

    public void updateScore() {
        // 修改学生成绩
        System.out.print("请输入您要修改的学生id:");
        int id = INPUT.nextInt();
        //判断id是否存在
        // 遍历集合
        int flag = -1;
        for (int i = 0; i < stu.size(); i++) {
            if (id == stu.get(i).getId()) {
                flag = i;
            }
        }

        if (flag == -1) {
            System.out.println("您输入的id不存在");
            return;
        }

        if (flag != -1) {
            System.out.print("请输入您要修改学生成绩:");
            int score = INPUT.nextInt();
            stu.get(flag).setScore(score);
            System.out.println("修改成功");
        }
        Select();
    }

    public void delete() {
        System.out.print("请输入您要删除的学生id:");
        int id = INPUT.nextInt();
        for (int i = 0; i < stu.size(); i++) {
            if (stu.get(i).getId() == id) {
                stu.remove(i);
            }
        }
        Select();
    }
    //对学生成绩进行排序
    public void sort() {
        for (int i = 0; i < stu.size(); i++) {
            for (int j = i+1; j < stu.size()-1; j++) {
                if(stu.get(i).getScore()>stu.get(j).getScore()){
                    Student temp=stu.get(i);
                    stu.set(i,stu.get(j));
                    stu.set(j,temp);
                }
            }
        }
        Select();
    }

    //按年龄进行排序
    public void sortAge() {
        for (int i = 0; i < stu.size(); i++) {
            for (int j = i+1; j < stu.size()-1; j++) {
                if(stu.get(i).getAge()>stu.get(j).getAge()){
                    Student temp=stu.get(i);
                    stu.set(i,stu.get(j));
                    stu.set(j,temp);
                }
            }
        }
        Select();
    }
}

